package deviceMng

import (
	"io/ioutil"
	"net/http"
	"strings"
)

type DeviceOptionHttp struct {
	Url string
}

func NewDeviceOptionHttp(url string) *DeviceOptionHttp {
	return &DeviceOptionHttp{
		Url: url,
	}
}

func (do *DeviceOptionHttp) Get(data string) (string, error) {
	req, err := http.NewRequest(http.MethodGet, do.Url, strings.NewReader(data))
	if err != nil {
		return "", err
	}
	r, err1 := http.DefaultClient.Do(req)
	if err1 != nil {
		return "", err
	}

	body, _ := ioutil.ReadAll(r.Body)
	r.Body.Close()
	return string(body), nil
}

func (do *DeviceOptionHttp) Post(data string) (string, error) {
	r, err := http.Post(do.Url, "application/json", strings.NewReader(data))
	if err != nil {
		return "", err
	}
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return "", err
	}
	r.Body.Close()
	return string(body), nil
}
